package com.wyh.service.serviceImpl.groupOne.customer;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.exception.CustomException;
import com.wyh.domain.po.groupOne.customer.MasterDataContacts;
import com.wyh.domain.vo.groupOne.customer.CustomerAccuratelyVO;
import com.wyh.domain.vo.groupOne.customer.MasterDataCustomerVO;
import com.wyh.domain.vo.groupOne.customer.MasterDataProjectVO_1;
import com.wyh.mapper.groupOne.customer.MasterDataCCustomerMapper;
import com.wyh.mapper.groupOne.customer.MasterDataContactsMapper;
import com.wyh.service.groupOne.customer.MasterDataCCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;

@Service
public class MasterDataCCustomerServiceImpl implements MasterDataCCustomerService {

    @Autowired
    private MasterDataCCustomerMapper masterDataCCustomerMapper;

    @Autowired
    private MasterDataContactsMapper masterDataContactsMapper;

    @Override
    public List<MasterDataCustomerVO> selectAll(MasterDataCustomerVO customer) {
        return masterDataCCustomerMapper.selectAll(customer);
    }

    @Override
    public List<MasterDataCustomerVO> selectAccurately(CustomerAccuratelyVO customerVO) {

        List<MasterDataCustomerVO> dbcustomers = masterDataCCustomerMapper.selectAccurately(customerVO);
        for (MasterDataCustomerVO dbcustomer : dbcustomers) {
            if (ObjectUtil.isNotEmpty(dbcustomer)) { //当查询到的客户不为空时才能查到联系人
                List<MasterDataContacts> masterDataContactsList = masterDataContactsMapper.selectAll(dbcustomer.getId());
                if (!masterDataContactsList.isEmpty()) {
                    dbcustomer.setMasterDataContactsList(masterDataContactsList);
                }
            }
        }
        return dbcustomers;
    }


    /**
     * 分页查询
     */
    public PageInfo<MasterDataCustomerVO> selectPage(CustomerAccuratelyVO customer, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MasterDataCustomerVO> list = masterDataCCustomerMapper.selectAccurately(customer);
        for (MasterDataCustomerVO dbcustomer : list) {
            if (ObjectUtil.isNotEmpty(dbcustomer)) { //当查询到的客户不为空时才能查到联系人
                List<MasterDataContacts> masterDataContactsList = masterDataContactsMapper.selectAll(dbcustomer.getId());
                if (!masterDataContactsList.isEmpty()) {
                    dbcustomer.setMasterDataContactsList(masterDataContactsList);
                }
            }
        }
        return PageInfo.of(list);
    }



    @Override
    public int insert(MasterDataCustomerVO customer) {

        List<MasterDataCustomerVO> dbcustomers = masterDataCCustomerMapper.selectByName(customer.getName());
        List<MasterDataCustomerVO> dbcreditcodes = masterDataCCustomerMapper.selectByCreditcode(customer.getCreditcode());
        List<MasterDataCustomerVO> dbtelephones = masterDataCCustomerMapper.selectByTelephone(customer.getTelephone());
        if (customer.getName().isEmpty() || customer.getTelephone().isEmpty() || customer.getCreditcode().isEmpty() || customer.getType() == null) {
            throw new CustomException("4001", "用户名、手机号、社会信用码、客户类型不能为空");
        }

        for (MasterDataCustomerVO dbcustomer : dbcustomers) {
            if (ObjectUtil.isNotEmpty(dbcustomer)) {
                throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
            }
        }

        // 校验客户名称长度
        if (customer.getType() == 0) {
            if (customer.getName().length() < 4 || customer.getName().length() > 50) {
                throw new CustomException("400", "客户名称长度应为4至50字符");
            }
        }

        for (MasterDataCustomerVO dbcreditcode : dbcreditcodes) {
            // 判断统一社会信用码在集团公司内是否唯一
            if (ObjectUtil.isNotEmpty(dbcreditcode)) {
                throw new CustomException("400", "统一社会信用码重复");
            }
        }

        for (MasterDataCustomerVO dbtelephone : dbtelephones) {
            if (ObjectUtil.isNotEmpty(dbtelephone)) {
                throw new CustomException("400", "手机号重复");
            }
        }

        if (customer.getUserid() == null) {
            throw new CustomException(ResultCodeEnum.OPERATION_AUTHORITY_ERROR);
        }
        customer.setLastupdatetime(DateUtil.date().toString());
        return masterDataCCustomerMapper.insert(customer);

    }

    @Override
    public MasterDataCustomerVO selectDetailBasic(Integer id) {
        if (id == null) {
            throw new CustomException(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        return masterDataCCustomerMapper.selectDetailBasic(id);
    }

    @Override
    public int update(MasterDataCustomerVO customer) {
        MasterDataCustomerVO dbcustomer = masterDataCCustomerMapper.selectDetailBasic(customer.getId());
        if (!ObjectUtil.isEmpty(dbcustomer)) {
            DateTime date = DateUtil.date();
            customer.setLastupdatetime(date.toString());
            if (dbcustomer.getState() == 1) {
                return masterDataCCustomerMapper.update(customer);
            }
            throw new CustomException(ResultCodeEnum.STATE_ERROR);
        }
        return 0;

    }

    @Override
    public int updateState(MasterDataCustomerVO masterDataCustomerVO) {
        Integer state = masterDataCustomerVO.getState();
        MasterDataCustomerVO dbcustomer = masterDataCCustomerMapper.selectDetailBasic(masterDataCustomerVO.getId());
        if (!ObjectUtil.isEmpty(dbcustomer)) {
            if (state == 0 || state == 1) {
                DateTime date = DateUtil.date();
                dbcustomer.setLastupdatetime(date.toString());
                dbcustomer.setUserid(masterDataCustomerVO.getUserid());
                masterDataCCustomerMapper.update(dbcustomer);
                return masterDataCCustomerMapper.updateState(masterDataCustomerVO);
            }
        }

        throw new CustomException(ResultCodeEnum.STATE_ERROR);
    }

    @Override
    public List<MasterDataCustomerVO> selectByOther(String input) {
        Pattern phone = Pattern.compile("^\\d{11}$");
        Pattern idnumber = Pattern.compile("^[a-zA-Z0-9]{15}|[a-zA-Z0-9]{18}|[a-zA-Z0-9]{20}$");

        if (phone.matcher(input).matches()) {
            return masterDataCCustomerMapper.selectByTelephone(input);
        } else if (idnumber.matcher(input).matches()) {
            return masterDataCCustomerMapper.selectByCreditcode(input);
        } else {
            return masterDataCCustomerMapper.selectByName(input);
        }
    }

    @Override
    public List<MasterDataProjectVO_1> selectAllName() {
        return masterDataCCustomerMapper.selectAllName();
    }
}
